package com.jxwifi.cloud.quickcleanserver.mycenter;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.basicactivity.BasicActivity;
import com.idroid.widget.Toaster;
import com.jxwifi.cloud.quickcleanserver.R;
import com.jxwifi.cloud.quickcleanserver.app.AccountMgr;
import com.jxwifi.cloud.quickcleanserver.app.CleanBasicActivity;
import com.jxwifi.cloud.quickcleanserver.bean.InfoBean;
import com.jxwifi.cloud.quickcleanserver.presenter.ContactPresenter;
import com.jxwifi.cloud.quickcleanserver.view.ContactView;
import com.jxwifi.cloud.quickcleanserver.utils.PasswordLogin;
import com.jxwifi.cloud.quickcleanserver.utils.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.trinea.android.common.util.REPattern;

/**
 * Created by Administrator on 2019/5/21.
 */

public class ContactTellActivity extends CleanBasicActivity implements ContactView {

    @Bind(R.id.et_my_personal_data_name)
    EditText mEtMyPersonalDataName;
    private ContactPresenter mPresenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_tell);
        ButterKnife.bind(this);
        openEventBus();
        initView();
    }

    public void initView() {
        mEtMyPersonalDataName.setText(AccountMgr.getcontactTell(mBaseContext));
        mPresenter=new ContactPresenter(this);
    }
    @OnClick({R.id.rel_img_back_pressed, R.id.tv_working_confirm})
    void clickCK(View v) {
        switch (v.getId()) {
            case R.id.rel_img_back_pressed:
                onBackPressed();
                break;
            case R.id.tv_working_confirm:
                if (!REPattern.isMobileNO(mEtMyPersonalDataName.getText().toString())) {
                    Toaster.show(mBaseContext, "请输入正确的手机号码");
                    return;
                }
                mPresenter.doContactTell();
                break;
        }
    }

    @Override
    public String getTell() {
        return mEtMyPersonalDataName.getText().toString();
    }

    @Override
    public void ContactFailureMsg(int errorCode, String msg) {
        Toast toast =Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast,3000);
    }

    @Override
    public void ContactSuccessMsg(int code, String data) {
        if (code == 0) {
            InfoBean mInfoBean = JSON.parseObject(data, InfoBean.class);
            AccountMgr.putUser(mBaseContext, mInfoBean,false);
            finish();
        } else {
            PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
            if (PasswordLogin.mIsLogin){
                mPresenter.doContactTell();
            }
        }
    }
}
